package thread.springAsync;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

/**
 * 一、Async 测试类<br/>
 * 1、spring 异步方法测试类<br/>
 * 
 * @Date 2016/08/29
 * @author liyl
 */
@Service
public class DaoService {

	/**
	 * 无返回值调用<br/>
	 * 
	 * @parameter
	 * @return
	 */
	@Async
	public void update() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("operation complete.");
	}

	/**
	 * 有返回值调用方式<br/>
	 * 
	 * @parameter
	 * @return
	 * @throws
	 */
	@Async
	public Future<String> asyncMethodWithReturnType() {
		System.out.println("Execute method asynchronously - " + Thread.currentThread().getName());
		try {
			Thread.sleep(5000);
			return new AsyncResult<String>("hello world !!!!");
		} catch (InterruptedException e) {
			//
		}
		return null;
	}

	@Async
	public void throwException() {
		try {
			Thread.sleep(5000);
			System.out.println("抛异常啦------------------------");
			throw new RuntimeException("error");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
